package com.crois.barrier.admin.controller;

import com.crois.barrier.admin.dto.ApiRouteGroupDto;
import com.crois.barrier.admin.entity.ApiRouteGroup;
import com.crois.barrier.admin.mapstruct.ApiRouteGroupDtoEntityMapper;
import com.crois.barrier.admin.resp.BarrierResponseWrapper;
import com.crois.barrier.admin.service.IApiRouteGroupSvc;
import com.crois.barrier.common.utils.UtilsAndCommons;
import com.crois.barrier.common.response.BarrierResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 12:12 2021/10/25
 */

@RequestMapping(UtilsAndCommons.DEFAULT_BARRIER_ADMIN_API_ROUTE_GROUP_CONTEXT)
@RestController
public class ApiGroupCtl {

    @Autowired
    @Qualifier("apiRouteGroupSvc")
    private IApiRouteGroupSvc apiRouteGroupSvc;


    /***
     * barrier api route group save
     */
    @PostMapping(UtilsAndCommons.BARRIER_ADD_CONTEXT)
    public BarrierResponseEntity<Void> apiRouteGroupAdd(@RequestBody ApiRouteGroupDto apiRouteGroupDto){
        apiRouteGroupSvc.save(ApiRouteGroupDtoEntityMapper.INSTANCE.toApiRouteGroupEntity(apiRouteGroupDto));
        return BarrierResponseWrapper.ok();
    }

    /***
     * barrier api route group update
     */
    @PutMapping(UtilsAndCommons.BARRIER_UPDATE_CONTEXT+"/{apiRouteGroupId}")
    public BarrierResponseEntity<Void> apiRouteGroupUpdate(@PathVariable("apiRouteGroupId") Integer apiRouteGroupId,@RequestBody ApiRouteGroupDto apiRouteGroupDto){
        apiRouteGroupDto.setId(apiRouteGroupId);
        apiRouteGroupSvc.updateById(ApiRouteGroupDtoEntityMapper.INSTANCE.toApiRouteGroupEntity(apiRouteGroupDto));
        return BarrierResponseWrapper.ok();
    }

    /***
     * barrier api route group delete
     */
    @DeleteMapping(UtilsAndCommons.BARRIER_DELETE_CONTEXT+"/{apiRouteGroupId}")
    public BarrierResponseEntity<Void> apiRouteGroupDelete(@PathVariable("apiRouteGroupId") Integer apiRouteGroupId){
        apiRouteGroupSvc.removeById(apiRouteGroupId);
        return BarrierResponseWrapper.ok();
    }

    /***
     * barrier api route group list
     */
    @GetMapping(UtilsAndCommons.BARRIER_LIST_CONTEXT)
    public BarrierResponseEntity<List<?>> apiRouteGroupList(@RequestParam(value = "apiGroupName",required = false) String apiGroupName){
        List<ApiRouteGroup> apiRouteGroupList = apiRouteGroupSvc.findApiRouteGroupList(apiGroupName);
        return BarrierResponseWrapper.okList(ApiRouteGroupDtoEntityMapper.INSTANCE.toApiRouteGroupDtoList(apiRouteGroupList));
    }

    /***
     * barrier api route group detail
     */
    @GetMapping(UtilsAndCommons.BARRIER_DETAIL_CONTEXT+"/{apiRouteGroupId}")
    public BarrierResponseEntity<?> apiRouteGroupDetail(@PathVariable("apiRouteGroupId") Integer apiRouteGroupId){
         ApiRouteGroup apiRouteGroup = apiRouteGroupSvc.getById(apiRouteGroupId);
         return BarrierResponseWrapper.ok(ApiRouteGroupDtoEntityMapper.INSTANCE.toApiRouteGroupDto(apiRouteGroup));
    }

}
